package com.cn.onetrip.activity;

import com.cn.onetrip.adapter.MoreItemAdapter;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.NetworkManager;
import com.cn.onetrip.untility.Untility;
import com.cn.onetrip.view.AdView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MoreActivity extends Activity implements OnItemClickListener {
	// public static View view;
	private ListView moreList;
	private MoreItemAdapter adapter;
	private Button quit;
	private AdView adView;
	private String feedBackUrl = "http://tm.1trip.com/feedback";
	private String stateUrl = "http://tm.1trip.com/state";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Window w = getWindow();
		// view = w.getDecorView();
		setContentView(R.layout.activity_more);
		moreList = (ListView) findViewById(R.id.MoreList);
		adView = (AdView) findViewById(R.id.ad_view);
		adapter = new MoreItemAdapter(MoreActivity.this, moreList, handler);
		moreList.setAdapter(adapter);
		moreList.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// view.startAnimation(Untility.rQuest1Animation);
				// GroupActivity.changeFirstView();
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
	}

	@Override
	protected void onResume() {
		loadDate();
		adView.startTimer();
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		adapter.clean();
		adView.stopTimer();
	}

	public void loadDate() {
		adapter.clean();
		adapter.addTitleText(R.string.more_user_protocol);
		adapter.addTitleText(R.string.more_suggest);

		// adapter.addTitleText(R.string.more_other_app);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(MoreActivity.this, MoreWebActivity.class);
		Bundle bundle = new Bundle();
		switch (arg2) {
		case 0:
			bundle.putString("webName", "state");
			bundle.putString("webUrl", stateUrl);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			break;
		case 1:
			if (!NetworkManager.CheckNetworkState(getApplicationContext())) {
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.network_fail),
						Toast.LENGTH_LONG).show();
				return;
			}
			bundle.putString("webName", "feedback");
			bundle.putString("webUrl", feedBackUrl);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			break;
		case 2:
			bundle.putString("webName", "App");
			bundle.putString("webUrl", feedBackUrl);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	public Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			if (msg.what == 0) {
			}
		};
	};
}
